package com.example.clientebabymonitorsoap;


import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


import com.example.clientebabymonitorsoap.R;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MostrarCuestionarios extends Activity{
	
	
	final String ID = "id1";
	private String URL = "http://172.22.197.230:8080/ServicioBM/services/WebServiceBabyMonitor";
	
	private String NAMESPACE = "http://DefaultNamespace";

	private TextView txtResultadoServicio;
	private ListView listCuestionarios;
	private ArrayList<String> item = new ArrayList<String>();
	private Button btnCuestionarios;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mostrarcuestionarios);
        
        txtResultadoServicio = (TextView) findViewById(R.id.txt_resultado);
        btnCuestionarios = (Button) findViewById(R.id.btn_cuestionarios);
        new ObtenerCuestionariosBackground().execute();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void obtenerCuestionarios(View v){
    	//new ObtenerCuestionariosBackground().execute();
    	
    	listCuestionarios = (ListView) findViewById(R.id.list_cuestionarios);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, item);
        listCuestionarios.setAdapter(adapter);
        
     // listening to single list item on click
        listCuestionarios.setOnItemClickListener(new OnItemClickListener() {
          
        	public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        	  
        	  
        	  //String item = (String) getListAdapter().getItem(position);
              // selected item
              //String product = ((TextView) view).getText().toString();
 
              // Launching new Activity on selecting single List Item
              Intent a = new Intent(MostrarCuestionarios.this, MostrarPreguntas.class);
              a.putExtra("opcion", listCuestionarios.getPositionForView(view)+1);
              startActivity(a);
              //sending data to new activity
              //i.putExtra("product", product);
              //startActivity(i);
 
          }
        });
    	
    	btnCuestionarios.setEnabled(false);
    }

    class ObtenerCuestionariosBackground extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {

        	String resultadoJSON="";
            String METHOD_NAME = "obtenerCuestionarios";
            String SOAP_ACTION = NAMESPACE +"/"+ METHOD_NAME;
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.implicitTypes =true;
            soapEnvelope.setOutputSoapObject(request);
            
            HttpTransportSE aht = new HttpTransportSE(URL);
            
            Log.d("ClienteBabyMonitor SOAP", "Iniciando Consulta");
            try {
                    aht.call(SOAP_ACTION, soapEnvelope);
                    SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
                    resultadoJSON = resultString.toString();
                    Log.d("ClienteBabyMonitor SOAP", "Finalizando Consulta");
                    
            } catch (IOException e) {
                    e.printStackTrace();
                    
            } catch (XmlPullParserException e) {
                    e.printStackTrace();
            }

            return resultadoJSON;
        }

        @Override
        protected void onPostExecute(String result) {
                super.onPostExecute(result);
                try {
                        String texto = "";
                        JSONObject resultado = new JSONObject(result);
                        //texto += resultado.getString("Titulo") +"\n";
                        String contenido = resultado.getString("Contenido");
                        Log.d("ClienteBabyMonitorSoap", contenido);
                        JSONArray peliculasJSONArray = new JSONArray(contenido);
                        int cantCuest = peliculasJSONArray.length();
                        for(int i = 0; i< cantCuest; i ++){
                                //texto += peliculasJSONArray.getJSONObject(i).getString("idCuestionario") +"\n";
                                //texto += peliculasJSONArray.getJSONObject(i).getString("rutMedico") +"\n";
                                texto += peliculasJSONArray.getJSONObject(i).getString("descripcion") +1;
                                //texto += peliculasJSONArray.getJSONObject(i).getString("fecha") +"\n";
                                item.add(peliculasJSONArray.getJSONObject(i).getString("descripcion"));
                        }
                        
                        txtResultadoServicio.setText(texto);
                        
                        
                } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                
        }
        
    }
}